#!/bin/bash

repoList=$1
request_repo=$2
requestDir=$3
userReq=$4 #we need to filter out review comments made by submitter themselves.
#resDir="topRepoCmts"
resDir="testtopRepoCmts"
mkdir -p $resDir

#run e.g., ./run_retrieveComments.sh repo_requestCnt.res request_repo.res reviews2017 formatted_user_requests
awk -F ',' '{if($2>=1000 && $1!="group")print $1}' $repoList > selectedRepo
head="request_id邹role邹who邹when邹comment"
for repo in $(cat selectedRepo)
do

    #if [ ! -f "${resDir}/${repo}_all_comments.res" ]; then
    #    echo $head > ${resDir}/${repo}_all_comments.res
    #fi

    #if [ ! -f "${resDir}/${repo}_top_comments.res" ]; then
    #    echo $head > ${resDir}/${repo}_top_comments.res
    #fi

    if [ ! -f "${resDir}/${repo}_inline_comments.res" ]; then
        echo $head > ${resDir}/${repo}_inline_comments.res
    fi

    #awk -F "邹" '{print $1".html"}' ${resDir}/${repo}_all_comments.res \
    awk -F "邹" '{print $1".html"}' ${resDir}/${repo}_inline_comments.res \
        | sort -u > donereq
    grep -E ", $repo$" $request_repo | cut -f1 -d "," > allreq
    awk '{if(NR<=FNR){arr[$1]=1}else{if(!($1 in arr))print $1}}' donereq allreq > tmprequests

    for rid in $(cat tmprequests)
    do
        id=$(echo $rid | cut -f1 -d ".")
        submitter=$(grep -E ",${id}," $userReq | cut -f2 -d ",")
        #python retrieve_requestComments.py ${requestDir}/${rid} 0 $submitter \
        #    >> ${resDir}/${repo}_all_comments.res

        #python retrieve_requestComments.py ${requestDir}/${rid} 1 $submitter \
        #    >> ${resDir}/${repo}_top_comments.res

        python retrieve_requestComments.py ${requestDir}/${rid} 2 $submitter \
            >> ${resDir}/${repo}_inline_comments.res
    done
done
